const express = require('express');
const router = express.Router();
const sys_role = require('../../../models/sys_role')

router.post('/sys/role/paging', async (req, res) => {
    try {
        const { current, size } = req.body

        const allRoles = await sys_role.findAll()
        const roles = await sys_role.findRolesByPage(current, size)

        const records = roles.map(role => ({
            id: role.id,
            roleName: role.role_name
        }))

        return res.json({
            "msg": "操作成功！",
            "code": 0,
            "data": {
                "records": records,
                "total": allRoles.length,
                "size": size,
                "current": current,
                "orders": [],
                "optimizeCountSql": true,
                "hitCount": false,
                "searchCount": true,
                "pages": Math.ceil(allRoles.length / size)
            },
            "success": true
        })
    } catch (error) {
        console.error(error);
        return res.status(500).json({ message: 'Internal server error' });
    }
})


router.post('/sys/role/list', async (req, res) => {
    try {
        const allRoles = await sys_role.findAll()

        const records = allRoles.map(role => ({
            id: role.id,
            roleName: role.role_name
        }))

        return res.json({
            "msg": "操作成功！",
            "code": 0,
            "data": records,
            "success": true
        })
    } catch (error) {
        console.error(error)
        return res.status(500).json({ message: 'Internal server error' })
    }
})

module.exports = router